package com.consumer.controller;

import com.consumer.feign.TestService;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URL;

@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private TestService testService;

    @GetMapping("/test/consumer/{name}")
    public String test(@PathVariable String name){
        String result = "";
        /*ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
        URI uri = serviceInstance.getUri();
        result = restTemplate.getForObject(uri + "/test/provider/" + name, String.class);*/
        // 服务调用，需要实现负载均衡
        //result = restTemplate.getForObject("http://service-provider/test/provider/"+name,String.class);

        result = testService.test(name);
        return result;
    }

    @GetMapping("/getUser")
    public User getUser(User user){
        return testService.getUser(user);
    }
}
